package work.timejay.handler;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import work.timejay.common.ApiResult;
import work.timejay.exception.TimeJayException;
import work.timejay.utils.LoginHelper;

import javax.servlet.http.HttpServletRequest;

/**
 * @className:ExceptionHandler.java
 * @author:王文杰
 * @version:1.0.0
 * @description:全局异常处理
 * @createTime 2023年11月17日 15:51:00
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    // 全局异常拦截
    @ExceptionHandler
    public SaResult handlerException(Exception e) {
        e.printStackTrace();
        return SaResult.error(e.getMessage());
    }

    // 全局异常拦截
    @ExceptionHandler(value = TimeJayException.class)
    public ApiResult<Object> handlerException(TimeJayException e) {
        e.printStackTrace();
        return ApiResult.error(e.getParams()[0],e.getCode()) ;
    }

    @ExceptionHandler(value = NotLoginException.class)
    public ApiResult<Object> handlerException(NotLoginException e, HttpServletRequest request) {
        log.error("message--->{},requestPath--->{}",e.getMessage(),request.getRequestURI());
        return ApiResult.error("认证失败，无法访问系统资源",HttpStatus.HTTP_UNAUTHORIZED) ;
    }
}
